$(function() {
	if(getCookieValue("token")==""){
		window.location.href = 'l-userlogin.html'
	}
	
	queryAllCategory(function(data) {
		$('.icon-contain-list').html(template('iconlistTemplate', data));
		var longClick = false;
		$(".icon-list").on({
			//长按显示删除键
			touchstart: function(e) {
				var that = $(this);
				longClick = false; //设置初始为false
				timeOutEvent = setTimeout(function() {
					//此处为长按事件-----在此显示遮罩层及删除按钮         
					//console.log(222)
					$('.link-delete').stop().fadeOut();
					that.children().eq(0).children().eq(1).stop().fadeIn();
					longClick = true; //假如长按，则设置为1
				}, 1000);
			},
			//结束清除定时器
			touchend: function(e) {
				clearTimeout(timeOutEvent);
			}
		});
		
		$('.icon-contain-list').click(function(){
			$('.link-delete').stop().fadeOut();
		});
		
		//点击跳转修改
		$(".icon-list").click(function() {
			var editLinkId = $(this).attr('data-id');
			window.location.href = 'a-link-editlink.html?id='+editLinkId;
		});
		
		//点击删除
		$('.link-delete').click(function() {
			var deleteLinkId = $(this).attr('data-id');
			console.log(deleteLinkId);
			var clickNow = $(this).parents('.icon-list');
			deleteLink({
				id:deleteLinkId
			},function(data){
				clickNow.stop().fadeOut();
			});
			return false;
		});
			
	});

	//防止手机键盘破坏布局
	var win_h = $(window).height(); //关键代码
	window.addEventListener('resize', function() {
		if($(window).height() < win_h) {
			$('.code-btn').hide();
		} else {
			$('.code-btn').show();
		}
	});
	
	//创建跳转
	$('.code-btn').on('tap',function(){
		window.location.href = 'a-link-newlink.html?business='+newBusinessL;
	});
	
});

//查询所有链接
var newBusinessL = CD.geturl().business;
if(!newBusinessL){
	newBusinessL=''
}
var queryAllCategory = function(callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/link/selectByUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			'userUuid':newBusinessL
		},
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
};

//删除分类
var deleteLink = function(params,callback){
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/link/deleteById",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data:params,
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}


